// httpsqsclient
package main

import (
	"flag"
	"fmt"
	"io/ioutil"
	"log"
	"net"
	"net/http"
	"net/url"
	"strings"
	"time"
)

const (
	ERROR           = "HTTPSQS_ERROR"
	GET_END         = "HTTPSQS_GET_END"
	PUT_OK          = "HTTPSQS_PUT_OK"
	PUT_ERROR       = "HTTPSQS_PUT_ERROR"
	PUT_END         = "HTTPSQS_PUT_END"
	RESET_OK        = "HTTPSQS_RESET_OK"
	RESET_ERROR     = "HTTPSQS_RESET_ERROR"
	MAXQUEUE_OK     = "HTTPSQS_MAXQUEUE_OK"
	MAXQUEUE_CANCEL = "HTTPSQS_MAXQUEUE_CANCEL"
	SYNCTIME_OK     = "HTTPSQS_SYNCTIME_OK"
	SYNCTIME_CANCEL = "HTTPSQS_SYNCTIME_CANCEL"
	Ip              = "192.168.11.26:1218"
	Proxy           = "127.0.0.1:8080"
)

var over = make(chan int)

func GetHttpsqsMsg(HttpsqsIp, PoolName string) (m []string, isok bool) {
	Url := "http://" + HttpsqsIp + "/?name=" + PoolName + "&opt=get"
	client := new(http.Client)
	reg, err := http.NewRequest("GET", Url, nil)
	if err != nil {
		log.Printf("Read httpsqs failure! err:%s\n", err)
		return
	}
	reg.Header.Set("Connection", "keep-alive")
	resp, err := client.Do(reg)
	if err != nil {
		log.Printf("Client.do failure! err:%s\n", err)
		return
	}
	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		fmt.Println("Error3:", err)
	}
	if string(body) != "HTTPSQS_GET_END" {
		m := strings.Split(string(body), ",")
		isok = true
	}
	return
}

func main() {
	for {
		go func() {
			GetHttpsqsMsg()
		}()
		<-over
		time.Sleep(500 * time.Millisecond)
	}
}
